package com.gitee.secretopen.sso.service.impl;

import cn.chenc.framework.cache.util.RedisUtils;
import cn.chenc.framework.core.exception.CustomException;
import cn.chenc.framework.core.util.Md5Util;
import cn.chenc.framework.core.util.SessionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.secretopen.sso.common.constant.Constants;
import com.gitee.secretopen.sso.component.TokenService;
import com.gitee.secretopen.sso.db.dao.SiteMapper;
import com.gitee.secretopen.sso.db.dao.UserMapper;
import com.gitee.secretopen.sso.db.pojo.Site;
import com.gitee.secretopen.sso.db.pojo.User;
import com.gitee.secretopen.sso.exception.CustomCode;
import com.gitee.secretopen.sso.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Xjt
 * @Date 2021/2/26 19:16
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private TokenService tokenService;

    @Override
    public Map<String, Object> ssoLogin(String name, String password, String code, String siteId) {
        Map<String, Object> results = new HashMap<>();
        String checkCode = SessionUtil.getKaptcha();
        Site site = siteMapper.selectOne(new QueryWrapper<Site>().eq("site_id", siteId));
        if (site == null) {
            throw new CustomException(new CustomCode(false,500,"非法应用！"));
        }
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name",name));
        if (user == null || user.getIsDelete() == 1 || !user.getPassword().equals(Md5Util.MD5(password,name))) {
            throw new CustomException(new CustomCode(false,500,"账号不存在或密码错误！"));
        }

        if (!checkCode.equals(code)) {
            throw new CustomException(new CustomCode(false,500,"验证码错误！"));
        }
        if (user == null || !user.getPassword().equals(Md5Util.MD5(password,name))) {
            throw new CustomException(new CustomCode(false,500,"账号不存在或密码错误！"));
        }
        user.setPassword(null);
        String token = tokenService.createToken();
        redisUtils.set(Constants.SSO_LOGIN_KEY+token,user);
        results.put("user",user);
        results.put("token",token);
        return results;
    }

    @Override
    public Map<String, Object> adminLogin(String name, String password, String code) {
        Map<String, Object> results = new HashMap<>();
        String checkCode = SessionUtil.getKaptcha();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name",name));
        if (!checkCode.equals(code)) {
            throw new CustomException(new CustomCode(false,500,"验证码错误！"));
        }
        if (user == null || !user.getPassword().equals(Md5Util.MD5(password,name))) {
            throw new CustomException(new CustomCode(false,500,"账号不存在或密码错误！"));
        }
        if (user.getIsLoginSsoAdmin() != 0) {
            throw new CustomException(new CustomCode(false,500,"权限不足！"));
        }
        user.setPassword(null);
        String token = tokenService.createToken();
        redisUtils.set(Constants.SSO_ADMIN_LOGIN_KEY+token,user);
        results.put("user",user);
        results.put("token",token);
        return results;
    }

    @Override
    public Object getUserByToken(String token) {
        Object user = redisUtils.get(Constants.SSO_LOGIN_KEY + token);
        if (user == null) {
            throw new CustomException(new CustomCode(false,500,"请重新登录！"));
        }
        return user;
    }

    @Override
    public boolean adminLogout(String token) {
        boolean result = redisUtils.hasKey(Constants.SSO_ADMIN_LOGIN_KEY + token);
        if (result) {
            redisUtils.del(Constants.SSO_ADMIN_LOGIN_KEY + token);
            return true;
        }
        return false;
    }

    @Override
    public boolean ssoLogout(String token) {
        boolean result = redisUtils.hasKey(Constants.SSO_LOGIN_KEY + token);
        if (result) {
            redisUtils.del(Constants.SSO_LOGIN_KEY + token);
            return true;
        }
        return false;
    }


}
